package com.wrial.kind.doublepointer;
/*
 * @Author  Wrial
 * @Date Created in 14:18 2020/8/7
 * @Description  需要注意大写
 * 输入: "hello"  输出: "holle"
 */

import java.util.Arrays;
import java.util.HashSet;

public class reverse字符串中元音字母 {

    /**
     * 双指针，一个从头一个从尾，需要注意的是大小写元音字母都算数
     * @param s
     * @return
     */
    public String reverseVowels(String s) {
        char[] chars = s.toCharArray();
        HashSet<Character> vowels = new HashSet<>(
        Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
        int left = 0, right = s.length() - 1;
        while (left < right) {
            if (vowels.contains(chars[left]) && vowels.contains(chars[right])) {
                char temp = chars[left];
                chars[left] = chars[right];
                chars[right] = temp;
                left++;
                right--;
            }
            if (!vowels.contains(chars[left])) left++;
            if (!vowels.contains(chars[right])) right--;
        }
        return new String(chars);
    }
}
